<?php
class MAmazonStatic extends MY_Model
{

    protected $table = 'amazon_static';
    const STATE_UNABLE = 0;
    const STATE_ABLE = 1;

    function __construct()
    {
        parent::__construct();
    }

    public function createTable($month){
        $date = str_replace('/','_',$month);
        $sql = "create table IF NOT EXISTS core_amazon_order_".$date." like core_amazon_order";
        $result = $this->db->query($sql);
        return $result;
    }

    public function getById($id)
    {
        $query = $this->db->where('id', $id)->get($this->table);
        return $query->row();
    }

    public function getAllStatic()
    {

        $queryResult = $this->getAll();
        $kvArr = array();
        foreach ($queryResult as $k => $v) {
            $kvArr[$v->id] = $v->name;
        }
        return $kvArr;
    }

    public static function getState($state = NULL)
    {
        $define = array(self::STATE_ABLE => '<span class="label label-success">可用</span>', self::STATE_UNABLE => '<span class="label label-danger">禁用</span>',);
        if ($state === NULL) {
            return $define;
        } else {
            return key_exists($state, $define) ? $define[$state] : '';
        }
    }

    public function getLast(){
        $this->db->select('*');
        $this->db->from($this->table);

        $this->db->order_by('id', 'desc');
        $query = $this->db->get();
        return $query->row();
    }

    public function getExportList($month, $where){

        $date = str_replace('/','_',$month);

        $table = 'core_amazon_order_'.$date;
        $this->db->select("*");
        $this->db->from($table);

        if ($begintime = $where['order_create_time_begin']) {
            $this->db->where(array('order_time >= ' => strtotime($begintime . ' 00:00:00')));
        }
        if ($endtime = $where['order_create_time_end']) {
            $this->db->where(array('order_time <= ' => strtotime($endtime . ' 23:59:59')));
        }
        if ($where['id']) {
            $this->db->where('id' ,$where['id']);
        }
        if ($where['shop_id']) {
            $this->db->where('shop_id', $where['shop_id']);
        }
        if ($where['group_id']) {
            $this->db->where('group_id', $where['group_id']);
        }
        if ($where['country']) {
            $this->db->where('country', $where['country']);
        }
        if ($where['coin_type']) {
            $this->db->where('coin_type', $where['coin_type']);
        }
        return $this->db->get()->result();

    }

    public function getOrderByDate($date,$limit){
        $table = 'core_amazon_order_'.$date;
        $this->db->select('*');
        $this->db->distinct();
        $this->db->from($table);

        $this->formfilter->doFilter();

        $this->db->limit($limit['limit'], $limit['offset']);
        $query = $this->db->get();
        return $query->result();
    }

    public function getOrderCount($date)
    {
        $table = 'core_amazon_order_'.$date;
        $this->db->select('count(distinct id) as num');
        $this->db->distinct();
        $this->db->from($table);

        $this->formfilter->doFilter();

        $tmp = $this->db->get()->result();
        return (int)$tmp[0]->num;
    }

    public function getList($limit, $type)
    {
        $this->db->select('s.*,a.name');
        $this->db->distinct();
        $this->db->from($this->table.' s');
        $this->db->join('admins a','s.user_id = a.id');
        $this->db->where('s.type',$type);

        $this->formfilter->doFilter();

        $this->db->limit($limit['limit'], $limit['offset']);
        $this->db->order_by('s.id', 'desc');
        $query = $this->db->get();
        return $query->result();
    }


    public function getCount($type)
    {
        $this->db->select('count(distinct s.id) as num');
        $this->db->distinct();
        $this->db->from($this->table.' s');
        $this->db->join('admins a','s.user_id = a.id');
        $this->db->where('s.type',$type);

        $this->formfilter->doFilter();
        $tmp = $this->db->get()->result();
        return (int)$tmp[0]->num;
    }

    public function getQuery($limit = null)
    {
        $this->db->select('*');
        $this->db->from($this->table);
        $this->db->where($this->table . '.del', UNDELETED);
        if ($limit != null)
            $this->db->limit($limit['limit'], $limit['offset']);
        $this->formfilter->doFilter();
        $rtn = $this->db->get();
        return $rtn;
    }

}
